秒杀项目学习笔记 第三章————秒杀功能开发及管理后台

秒杀项目学习笔记 第三章(秒杀功能开发及管理后台)

3-1 商品列表页的设计

1.表设计:分为商品表,订单表,秒杀商品表,秒杀订单表
分开是为了易于维护。

此处输入图片的描述

2.使用IDEA的数据表生成pojo功能:datebase→连接→Scripted Extensions→generatePojo

3.goodsService的会调用goodsDao.listGoodsVo方法取出来的是GoodsVo而不是Goods,GoodsVo内的属性是从数据库多表中联立取出。

4.取出的list加入到model中,在html中通过thymleaf循环取出

3-3 商品详情页的设计

彩蛋:数据库ID一般不用自增,容易被他人遍历,而用snowflake算法

前端部分:

1
<span th:if="${user eq null}"> 您还没有登录,请登陆后再操作<br/></span>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<tr>  
<td>秒杀开始时间</td>
<td th:text="${#dates.format(goods.startDate, 'yyyy-MM-dd HH:mm:ss')}"></td>
<td id="miaoshaTip">
<!-- 隐藏域临时保存${remainSeconds},在进行中和已结束也有remainSeconds,只是不显示-->
<input type="hidden" id="remainSeconds" th:value="${remainSeconds}" />
<span th:if="${miaoshaStatus eq 0}">秒杀倒计时:<span id="countDown" th:text="${remainSeconds}"></span></span>//倒计时的设计
<span th:if="${miaoshaStatus eq 1}">秒杀进行中</span>
<span th:if="${miaoshaStatus eq 2}">秒杀已结束</span>
</td>
<td>
<form id="miaoshaForm" method="post" action="/miaosha/do_miaosha">
<button class="btn btn-primary btn-block" type="submit" id="buyButton">立即秒杀</button>
<input type="hidden" name="goodsId" th:value="${goods.id}" />
</form>
</td>
</tr>

秒杀倒计时的设计:

1
<span th:if="${miaoshaStatus eq 0}">秒杀倒计时:<span id="countDown" th:text="${remainSeconds}"></span></span>//倒计时的设计

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<script>
$(function(){
countDown();
});

function countDown(){

var remainSeconds = $("#remainSeconds").val();
var timeout;

if(remainSeconds > 0){//秒杀还没开始,倒计时
$("#buyButton").attr("disabled", true);

timeout = setTimeout(function(){
$("#countDown").text(remainSeconds - 1);
$("#remainSeconds").val(remainSeconds - 1);
countDown();//回调函数,回调自己
},1000);
}else if(remainSeconds == 0){//秒杀进行中
$("#buyButton").attr("disabled", false);
if(timeout){
clearTimeout(timeout);
}
$("#miaoshaTip").html("秒杀进行中");
}else{//秒杀已经结束
$("#buyButton").attr("disabled", true);
$("#miaoshaTip").html("秒杀已经结束");
}
}
</script>

controller类里接收参数@PathVariable和@RequestParam的区别

1.@PathVariable 路径变量,是用来获得请求url中的动态参数的,用于将请求URL中的模板变量映射到功能处理方法的参数上。

th:href
1
2
3
4
5
6

然后
```Java
@RequestMapping("/to_detail/{goodsId}")
public String detail(Model model, MiaoshaUser user,
@PathVariable("goodsId") long goodsId){

2.@RequestParam :请求参数 用于接收request发来的参数

在SpringMVC后台控制层获取参数的方式主要有两种:
一种是request.getParameter(“name”),另外一种是用注解@RequestParam直接获取

1
2
@RequestMapping("/do_miaosha")
public String list(Model model, MiaoshaUser user, @RequestParam("goodsId") long goodsId){

接下来我们看一下@RequestParam注解主要有哪些参数
value:参数名字,即入参的请求参数名字,如username表示请求的参数区中的名字为username的参数的值将传入;
required:是否必须,默认是true,表示请求中一定要有相应的参数,否则将报404错误码;
defaultValue:默认值,表示如果请求中没有同名参数时的默认值,例如:

1
public List<EasyUITreeNode> getItemTreeNode(@RequestParam(value="id",defaultValue="0")long parentId)

3-4 秒杀方法的实现 秒杀需要user,和gooodsId

1.点击秒杀,传入

1
2
3
4
<form id="miaoshaForm" method="post" action="/miaosha/do_miaosha">
<button class="btn btn-primary btn-block" type="submit" id="buyButton">立即秒杀</button><!-- 只有在进行中才可以点击-->
<input type="hidden" name="goodsId" th:value="${goods.id}" />
</form>

2.MiaoshaController只需要两个

  • 判断user是否为空,为空跳转到login
  • 判断商品是否还有库存,user是否已经秒杀过(这个用户是否有order)。是的话跳转到miaosha_fail
  • 调用miaoshaService。OrderInfo orderInfo = miaoshaService.miaosha(user, goods);

3.MiaoshaService

1
2
3
4
5
6
7
8
9
@Transactional//事务
public OrderInfo miaosha(MiaoshaUser user, GoodsVo goods) {

//减库存根据逻辑应该在goodsService中完成而不是在miaoshaService里完成
goodsService.reduceStock(goods);

//新建订单,返回一个orderInfo对象
return orderService.createOrder(user, goods);
}

3.1 goodsService减库存操作

1
goodsService.reduceStock(goods.getId());//使用了ID而没有像教程一样传入GoodsVo,会根据goodsId去减少Miaosha_goods中对应的库存

3.2 orderService新建订单操作
orderService中。先创建一个OrderInfo对象,然后根据GoodsVogoods对其设置其属性值。

1
2
//生成订单后插入orderInfo到order表里
long orderId = orderDao.insert(orderInfo);

再创建miaoshaOrder并设置其属性值,这里用到了生成的orderId,然后

1
2
3
//生成订单后插入miaoshaOrder到miaosha_order表里
orderDao.insertMiaoshaOrder(miaoshaOrder);
return orderInfo;

最后orderService返回订单,并MiaoshaService返回订单,MiaoshaController添加对象到视图中,跳转到order_detail显示属性。

1
2
3
4
5
OrderInfo orderInfo = miaoshaService.miaosha(user, goods);

model.addAttribute("orderInfo", orderInfo);
model.addAttribute("goods", goods);
return "order_detail";

3-5 订单详情页

很简单。在order_detail中取出显示出来即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<tr>
<td>订单状态</td>
<td >
<span th:if="${orderInfo.status eq 0}">未支付</span>
<span th:if="${orderInfo.status eq 1}">待发货</span>
<span th:if="${orderInfo.status eq 2}">已发货</span>
<span th:if="${orderInfo.status eq 3}">已收货</span>
<span th:if="${orderInfo.status eq 4}">已退款</span>
<span th:if="${orderInfo.status eq 5}">已完成</span>
</td>
<td>
<button class="btn btn-primary btn-block" type="submit" id="payButton">立即支付</button>
</td>
</tr>

Powered by Hexo and Hexo-theme-hiker

Copyright © 2017 - 2019 Jae's blog All Rights Reserved.

UV : | PV :